package com.lst.projectlib.component.datetimepicker;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;

import com.lst.projectlib.R;

public class SingleDatePickerActivity extends Activity implements
		OnClickListener, OnDateChangedListener {

	public static final String INTENT_BUNDLE_KEY_INITIAL_YEAR = "initial_year";
	public static final String INTENT_BUNDLE_KEY_INITIAL_MONTH = "initial_month";
	public static final String INTENT_BUNDLE_KEY_INITIAL_DAY = "initial_day";
	public static final String INTENT_BUNDLE_KEY_INITIAL_TITLE = "initial_title";
	public static final String INTENT_BUNDLE_KEY_SELECT_SINGLE_DATE = "select_single_date";

	private DatePicker mDatePicker;

	private int request_code = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_single_date_picker);

		TextView tv_ok = (TextView) this.findViewById(R.id.tv_ok);
		TextView tv_cancel = (TextView) this.findViewById(R.id.tv_cancel);
		tv_ok.setOnClickListener(this);
		tv_cancel.setOnClickListener(this);

		mDatePicker = (DatePicker) this.findViewById(R.id.date_picker);
		
		initData();
	}

	private void initData() {
		Intent intent = getIntent();

		if (intent != null) {
			int year = intent.getIntExtra(
					INTENT_BUNDLE_KEY_INITIAL_YEAR, 0);
			int month = intent.getIntExtra(
					INTENT_BUNDLE_KEY_INITIAL_MONTH, 0);
			int day = intent.getIntExtra(
					INTENT_BUNDLE_KEY_INITIAL_DAY, 0);
			
			// 系统拿到的月份会小1（应该是从0计算的，所以得自己做处理，减去1）
			month--;
			
			mDatePicker.init(year, month, day, this);
			
			request_code = intent.getIntExtra("request_code", 0);
//			txt_title.setText(intent
//					.getStringExtra(INTENT_BUNDLE_KEY_INITIAL_TITLE));
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.tv_cancel) {
			finish();
		} else if (v.getId() == R.id.tv_ok) {
			Calendar calOverTime = Calendar.getInstance();
			int yearCur = calOverTime.get(Calendar.YEAR);
			int monthCur = calOverTime.get(Calendar.MONTH);
			int dayCur = calOverTime.get(Calendar.DAY_OF_MONTH);
			
			if (yearCur > mDatePicker.getYear()) {
				Toast.makeText(this, "投放日期不能小于当前日期", Toast.LENGTH_SHORT).show();
				return;
			} 
			if (yearCur == mDatePicker.getYear()) {
				int month = mDatePicker.getMonth();
				if (monthCur > month) {
					Toast.makeText(this, "投放日期不能小于当前日期", Toast.LENGTH_SHORT).show();
					return;
				}
				if (monthCur == month) {
					if (dayCur > mDatePicker.getDayOfMonth()) {
						Toast.makeText(this, "投放日期不能小于当前日期", Toast.LENGTH_SHORT).show();
						return;
					}
				}
			}
			
			tryNotifyTimeSet();
			finish();
		}
	}

	private void tryNotifyTimeSet() {
		if (mDatePicker != null) {
			mDatePicker.clearFocus();

			Intent intent = new Intent();
			intent.putExtra("request_code", request_code);

			String year = String.valueOf(mDatePicker.getYear());
			
			// 系统拿到的月份会小1（应该是从0计算的，所以得自己做处理，加上1）
			int nMonth = mDatePicker.getMonth() + 1;
			String month = nMonth < 10 ? ("0" + nMonth) : String.valueOf(nMonth);
			
			String day = mDatePicker.getDayOfMonth() < 10 ? ("0" + mDatePicker
					.getDayOfMonth()) : String.valueOf(mDatePicker
					.getDayOfMonth());

			intent.putExtra(INTENT_BUNDLE_KEY_SELECT_SINGLE_DATE, year
					+ "/" + month + "/" + day);
			this.setResult(RESULT_OK, intent);
		}
	}

	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		
	}
}
